﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Camc.Quality.Migrations
{
    public partial class add_materialsConten : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropPrimaryKey(
                name: "PK_MaterialsChildNodes",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "MaterialType",
                table: "MaterialsNodes");

            migrationBuilder.DropColumn(
                name: "IsKeyPart",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "IsStandard",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "IsUsed",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "MaterialNo",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "MaterialType",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "Model",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "Remark",
                table: "MaterialsChildNodes");

            migrationBuilder.DropColumn(
                name: "Unit",
                table: "MaterialsChildNodes");

            migrationBuilder.RenameTable(
                name: "MaterialsChildNodes",
                newName: "MaterialsDescs");

            migrationBuilder.RenameColumn(
                name: "MaterialName",
                table: "MaterialsDescs",
                newName: "Materialname");

            migrationBuilder.AddColumn<Guid>(
                name: "MaterialsContentId",
                table: "MaterialsNodes",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "MaterialsNodeId",
                table: "MaterialsDescs",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

            migrationBuilder.AddPrimaryKey(
                name: "PK_MaterialsDescs",
                table: "MaterialsDescs",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "MaterialsContents",
                columns: table => new
                {
                    Id = table.Column<Guid>(nullable: false),
                    CreationTime = table.Column<DateTime>(nullable: false),
                    CreatorUserId = table.Column<long>(nullable: true),
                    LastModificationTime = table.Column<DateTime>(nullable: true),
                    LastModifierUserId = table.Column<long>(nullable: true),
                    IsDeleted = table.Column<bool>(nullable: false),
                    DeleterUserId = table.Column<long>(nullable: true),
                    DeletionTime = table.Column<DateTime>(nullable: true),
                    MaterialName = table.Column<string>(nullable: true),
                    MaterialNo = table.Column<string>(nullable: true),
                    Model = table.Column<string>(nullable: true),
                    MaterialType = table.Column<string>(nullable: true),
                    Unit = table.Column<string>(nullable: true),
                    IsUsed = table.Column<int>(nullable: false),
                    IsKeyPart = table.Column<int>(nullable: false),
                    IsStandard = table.Column<int>(nullable: false),
                    Remark = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MaterialsContents", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_MaterialsNodes_MaterialsContentId",
                table: "MaterialsNodes",
                column: "MaterialsContentId");

            migrationBuilder.CreateIndex(
                name: "IX_MaterialsDescs_MaterialsNodeId",
                table: "MaterialsDescs",
                column: "MaterialsNodeId");

            migrationBuilder.AddForeignKey(
                name: "FK_MaterialsDescs_MaterialsNodes_MaterialsNodeId",
                table: "MaterialsDescs",
                column: "MaterialsNodeId",
                principalTable: "MaterialsNodes",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_MaterialsNodes_MaterialsContents_MaterialsContentId",
                table: "MaterialsNodes",
                column: "MaterialsContentId",
                principalTable: "MaterialsContents",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_MaterialsDescs_MaterialsNodes_MaterialsNodeId",
                table: "MaterialsDescs");

            migrationBuilder.DropForeignKey(
                name: "FK_MaterialsNodes_MaterialsContents_MaterialsContentId",
                table: "MaterialsNodes");

            migrationBuilder.DropTable(
                name: "MaterialsContents");

            migrationBuilder.DropIndex(
                name: "IX_MaterialsNodes_MaterialsContentId",
                table: "MaterialsNodes");

            migrationBuilder.DropPrimaryKey(
                name: "PK_MaterialsDescs",
                table: "MaterialsDescs");

            migrationBuilder.DropIndex(
                name: "IX_MaterialsDescs_MaterialsNodeId",
                table: "MaterialsDescs");

            migrationBuilder.DropColumn(
                name: "MaterialsContentId",
                table: "MaterialsNodes");

            migrationBuilder.DropColumn(
                name: "MaterialsNodeId",
                table: "MaterialsDescs");

            migrationBuilder.RenameTable(
                name: "MaterialsDescs",
                newName: "MaterialsChildNodes");

            migrationBuilder.RenameColumn(
                name: "Materialname",
                table: "MaterialsChildNodes",
                newName: "MaterialName");

            migrationBuilder.AddColumn<string>(
                name: "MaterialType",
                table: "MaterialsNodes",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "IsKeyPart",
                table: "MaterialsChildNodes",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "IsStandard",
                table: "MaterialsChildNodes",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "IsUsed",
                table: "MaterialsChildNodes",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<string>(
                name: "MaterialNo",
                table: "MaterialsChildNodes",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "MaterialType",
                table: "MaterialsChildNodes",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Model",
                table: "MaterialsChildNodes",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Remark",
                table: "MaterialsChildNodes",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Unit",
                table: "MaterialsChildNodes",
                nullable: true);

            migrationBuilder.AddPrimaryKey(
                name: "PK_MaterialsChildNodes",
                table: "MaterialsChildNodes",
                column: "Id");
        }
    }
}
